<?php 

/**
 * 分页
 *
 * @author itwhen
 * @param array  $params
 * @param object $smarty
 * @return string
 */
function smarty_function_userpage ($params, &$smarty) 
{ 
    /* 
    @param  mixed   $num_data          - number of data
    @param  int     $num_limit 				- number of data each page 
    @param  int     $num_link            - max. number of links to show on one page (default: 5) 
    @param  int     $page_current            - current page number 
    @param  string  url             - baseurl to which the pagenumber will appended 
    @param  string  url_append          - text to append to url after pagenumber, e.g. "html" (default: "") 
    @param  string  txt_first           - text for link to first page (default: "&&") 
    @param  string  txt_prev            - text for link to previous page (default: "&") 
    @param  string  separator           - text to print between page numbers (default: "&|&") 
    @param  string  txt_next            - text for link to next page (default: "&") 
    @param  string  txt_last            - text for link to last page (default: "&&") 
    @param  string  txt_skip            - text shown when page s are skipped (not shown) (default: "&...&") 
    @param  string  css_class           - css class for the pager (default: "") 
    @param  boolean link_current        - whether to link the current page (default: false) 
    */ 
          
    /* Define all vars with default value */ 
    $num_link = 7; 
    $url_append= ''; 
    $txt_first = '|&lsaquo;'; 
    //$txt_prev  = '&lsaquo;&lsaquo;'; 
    $txt_prev  = '上一页'; 
    //$txt_next  = '&rsaquo;&rsaquo;'; 
    $txt_next  = '下一页'; 
    $txt_last  = '&rsaquo;|'; 
    $link_current = false;

    extract($params); 
    
    $target = isset($target) ? " target=\"$target\"" : '';  

    /* Check parameters */ 
    if (empty($num_data)) {
        return; 
    } 
    if (empty($num_limit)) { 
        $smarty->trigger_error("sliding_pager: missing 'num_limit' parameter"); 
        return; 
    }
    if (empty($page_current)) { 
        $smarty->trigger_error("sliding_pager: missing 'page_current' parameter"); 
        return; 
    } 
    if (empty($url)) { 
        $url = '?page=';
    }else {
        if (strpos($url, 'page=')=== false) {
            if (strpos($url, '?')===false) {
                $url .= '?page=';
            } else {
                $url .= '&page=';
            }
        }
    } 
    
    $pagenum_data = ceil($num_data/$num_limit);

    if ($pagenum_data <= 1) {
        //return; 
    } 
    $page_current = (int)$page_current;
    if ($page_current < 1 || $page_current > $pagenum_data){
    	$page_current = 1; 
    }

    if ($num_link % 2 == 0) { 
        $dl = $num_link/2-1; 
        $dr = $num_link/2; 
    } else { 
        $dl = $dr = ($num_link-1)/2; 
    } 

    $page_start = $page_current - $dl;
    $page_end = $page_current + $dr;
    
    for (;$page_start<1;$page_start++,$page_end++);
    for (;$page_end>$pagenum_data;$page_end--,$page_start--);
    $page_start<=0 && $page_start = 1;
    
    $links = range($page_start, $page_end);
    $retval = '<li class="page-mun">';
    /* Build link 
                    	<a href="#" title="<<"><<</a>
                        <a href="#" title="1" class="default">1</a>
                        <a href="#" title="2">2</a>
                        <a href="#" title="3">3</a>
                        <a href="#" title="4">4</a>
                        <a href="#" title="5">5</a>
                        <a href="#" title="6">6</a>
                        <a href="#" title="7">7</a>
                        <a href="#" title="8">8</a>
                        <span>...</span>
                        <a href="#">>></a>
                    </li>
                    <li>共100页</li>
                    <li>到第</li>
                    <li style="padding:0;"><input type="text" class="pageinput" /></li>
                    <li>页</li>
                    </ul>
    */ 
    /*if ($page_current-$dl>2){
        $retval .= '<a href="'.$url.'1'.$url_append.'">'.$txt_first.'</a>'; 
    }*/
    if ($page_current > 1) { 
        $retval .= '<a href="'.$url.($page_current - 1).$url_append.'" >'.$txt_prev.'</a>'; 
    }
    $count = count($links);
    for($i = 0; $i < $count; $i++) { 
        if (($links[$i] != $page_current) || $link_current) { 
            $retval .= '<a href="'.$url.$links[$i].$url_append.'">'.$links[$i].'</a>'; 
        } else { 
            $retval .= '<a '.$css_current. $target. '>'. $links[$i]. '</a>'; 
        } 
    } 
    if ($page_current != $pagenum_data) { 
        $retval .= '<a href="'.$url.($page_current + 1).$url_append.'" >'.$txt_next.'</a>'; 
    } 
    /*if ($page_current<=$pagenum_data-$dr-1){
        $retval .= '<a href="'.$url.$pagenum_data.$url_append.'">'.$txt_last.'</a>';
    }*/
    $retval.= '<li>共' . $page_current .'/'. $pagenum_data .' 页</li><li>到第</li><li style="padding:0;"><input type="hidden" name="url" value='.$params['url'].' /><input type="text" name="page" class="pageinput" /></li><li>页</li><li><input type="submit" class="pagesubmit" value="确定" /></li>';
    return $retval; 
} 

/* vim: set expandtab: */ 
/* vim: set ts=4: */ 

?> 
